layui.use(['form', 'table', 'treetable', 'laydate'], function () {
    var $ = layui.jquery;
    var form = layui.form;
    var table = layui.table;
    var treetable = layui.treetable;
    var laydate = layui.laydate;
    var layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var SysMenu = {
        id: 'SysMenuTable'
    };

    var column = [
                    [
                        {type: 'numbers'},
                        {field: 'name', minWidth: 200, title: '菜单名称'},
                        {field: 'href', title: '菜单链接'},
                        {field: 'type', title: '类型', width: 120, align: 'center',
                            templet: function (object) {
                                if(object.type == 0){
                                    return '<span class="layui-badge layui-bg-green">后台菜单</span>';
                                }else if(object.type == 1){
                                    return '<span class="layui-badge">前台菜单</span>';
                                }else if(object.type == 2){
                                    return '<span class="layui-badge layui-bg-blue">情侣空间</span>';
                                }
                            }
                        },
                        {field: 'icon', title: '图标', width: 120, align: 'center',
                            templet: function (object) {
                                return "<i class='" + object.icon + "'></i>";
                            }
                        },
                        {field: 'isShow', title: '是否显示', width: 120, align: 'center',
                            templet: function (object) {
                                if(object.isShow == 0){
                                    return '<span class="layui-badge layui-bg-green">是</span>';
                                }else{
                                    return '<span class="layui-badge">否</span>';
                                }
                            }
                        },
                        {field: 'sort', width: 120, align: 'center', title: '排序号'},
                        {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
                        {templet: '#stateBtn', width: 140, align: 'center', title: '操作'}
                    ]
    ];

    // 渲染表格
    SysMenu.initTable = function(){
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'parentId',
            elem: '#' + SysMenu.id,
            url: '/sys/menu/getMenuListToManager',
            page: false,
            cols: column,
            done: function () {
                layer.closeAll();
            }
        });
    };

    SysMenu.initTable();

    /**
     * 点击添加按钮
     */
    $("#addBtn").on("click", function () {
        var content = layuimini.getHrefContent('/sys/menu/addOrEdit');
        var index = top.layer.open({
            title: '添加菜单',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['500px','530px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                layuimini.refresh();
            }
        });

        return false;
    });

    /**
     * 点击编辑按钮
     * @param id
     */
    SysMenu.edit = function(id){
        var content = layuimini.getHrefContent('/sys/menu/addOrEdit?id=' + id);
        var index = top.layer.open({
            title: '编辑菜单',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['500px','530px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                layuimini.refresh();
            }
        });

        return false;
    };

    /**
     * 点击删除按钮
     * @param id
     */
    SysMenu.delete = function(id){
        layer.open({
            title: '删除菜单'
            ,content: '是否确定删除该菜单？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/sys/menu/delete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        layuimini.refresh();
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };


    $('#btn-expand').click(function () {
        treetable.expandAll('#' + SysMenu.id);
    });

    $('#btn-fold').click(function () {
        treetable.foldAll('#' + SysMenu.id);
    });

    //监听工具条
    table.on('tool(' + SysMenu.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'del') {
            SysMenu.delete(data.id);
        } else if (layEvent === 'edit') {
            SysMenu.edit(data.id)
        }
    });
});